//
//  ViewController.m
//  Light
//
//  Created by 骁唐 on 15/8/19.
//  Copyright (c) 2015年 骁唐. All rights reserved.
//

#import "ViewController.h"
#import "StoryboardManager.h"
#import "HomeTwoViewController.h"
#import "SliderViewController.h"
#import "constantDefinition.h"
#import "AFNetworking.h"
#import "NetworkTool.h"
#import "UserConfig.h"
@interface ViewController ()

@end

@implementation ViewController
{
    UIAlertView *alert;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UserConfig *userConfig = [UserConfig sharingUserConfig];
    if(![[userConfig getHaveLogin] isEqualToString:@"login"])
    {//如果没有登录标记 直接跳转到登录界面
        self.window.rootViewController = [StoryboardManager loginWorkflowEnterpoint];
        [TJJ_SD_BLE_INSTRUCTIONS unBind:@"nokey"];//发送nokey
    }
    else
    {
        [self getToken];
        UIImageView * backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backImageView.image = [UIImage imageNamed:@"Default"];
        [self.view addSubview:backImageView];
    }
    [self.window makeKeyAndVisible];
}

#pragma mark -
#pragma mark 获取token
- (void)getToken {
    UserConfig *userConfig =[UserConfig sharingUserConfig];
    //传的参数
    NSDictionary *dic= @{@"Token":[userConfig getToken]};
    AFHTTPRequestOperationManager *httpManager = [AFHTTPRequestOperationManager manager];
    httpManager.requestSerializer = [[AFHTTPRequestSerializer alloc]init];
    httpManager.responseSerializer = [[AFHTTPResponseSerializer alloc]init];
    //地址
    [httpManager POST:GET_INFO parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *retDic = [NetworkTool toNSDictionary:responseObject];
        if ([retDic[@"a"] intValue] == 1)
        {
            HomeTwoViewController *homet = [[HomeTwoViewController alloc] init];
            SliderViewController *slider = [SliderViewController sharedSliderController];
            if (SCREEN_WIDTH > 320)
            {
                slider.LeftSContentOffset =  SCREEN_WIDTH-WIDTH_MAIN_6*SCREEN_SCALE;//SXW__设置侧边栏
            }
            else
            {
                slider.LeftSContentOffset =  SCREEN_WIDTH-WIDTH_MAIN*SCREEN_SCALE;//SXW__设置侧边栏
            }
            slider.LeftSContentScale = 1;//左视图缩小，1表示不缩小。
            UIStoryboard *userSt = [UIStoryboard storyboardWithName:@"User" bundle:nil];
            slider.LeftVC = [userSt instantiateViewControllerWithIdentifier:@"userViewId"];
            slider.MainVC = homet;
            AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
            UINavigationController *navigationVc = [[UINavigationController alloc]initWithRootViewController:slider];
            navigationVc.navigationBar.hidden = YES;
            delegate.window.rootViewController = navigationVc;
        }
        else if ([retDic[@"a"] intValue] == -1)
        {
            ALERT_VIEW(@"账号过期");
            self.window.rootViewController = [StoryboardManager loginWorkflowEnterpoint];
        }
        else if ([retDic[@"a"] intValue] == 0)
        {
            ALERT_VIEW(@"未知错误");
            self.window.rootViewController = [StoryboardManager loginWorkflowEnterpoint];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        ALERT_VIEW(@"网络连接错误");
        self.window.rootViewController = [StoryboardManager loginWorkflowEnterpoint];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
